package demo.hdz.mynotification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.RemoteInput;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

/**
 * @author hdz
 * @date 2019/02/27
 */
public class Notifications {

    public final static int NOTIFICATION_SAMPLE = 0;
    public final static int NOTIFICATION_ACTION = 1;
    public final static int NOTIFICATION_REMOTE_INPUT = 2;
    public final static int NOTIFICATION_BIG_PICTURE_STYLE = 3;
    public final static int NOTIFICATION_BIG_TEXT_STYLE = 4;
    public final static int NOTIFICATION_INBOX_STYLE = 5;
    public final static int NOTIFICATION_MEDIA_STYLE = 6;
    public final static int NOTIFICATION_MESSAGING_STYLE = 7;
    public final static int NOTIFICATION_PROGRESS = 8;
    public final static int NOTIFICATION_CUSTOM_HEADS_UP = 9;
    public final static int NOTIFICATION_CUSTOM = 10;



    public static void sendSimpleNotification(Context context, NotificationManager nm, Uri uri) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知
            Notification.Builder nb = new Notification.Builder(context, NotificationChannels.LOW)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Simple notification")
                    //设置通知内容
                    .setContentText("Demo for simple notification !")
                    //设置点击通知后自动删除通知
                    .setAutoCancel(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    .setSound(uri)
                    //设置滑动不删除通知，效果同Notification.FLAG_NO_CLEAR
                    .setOngoing(true);
            //发送通知
            nm.notify(NOTIFICATION_SAMPLE, nb.build());
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setContentTitle("Simple notification")
                    .setContentText("Demo for simple notification !")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置点击通知后自动删除通知，只有添加点击通知时的响应事件才会生效
                    .setAutoCancel(true)
                    //设置滑动不删除通知，效果同Notification.FLAG_NO_CLEAR
                    .setOngoing(true)
                    //设置notification的优先级，优先级越高的，通知排的越靠前，优先级低的，不会在手机最顶部的状态栏显示图标
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //等价于setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
                    //.setDefaults(Notification.DEFAULT_ALL);
                    .setSound(uri);
            Notification notification = builder.build();
            // 不能被清除
            //notification.flags |= Notification.FLAG_NO_CLEAR;
            nm.notify(NOTIFICATION_SAMPLE, notification);
        }
    }

    public static void sendSimpleNotification2(Context context, NotificationManager nm) {

        //创建点击通知时发送的广播
        Intent intent = new Intent(context, NotificationService.class);
        intent.setAction(Constant.ACTION_SIMPLE);
        //获取一个用于启动 Service 的 PendingIntent 对象
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);

        //创建删除通知时发送的广播
        Intent deleteIntent = new Intent();
        deleteIntent.setAction(Constant.ACTION);
        deleteIntent.putExtra(Constant.DATA, "简单的通知");
        PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context,0,deleteIntent,0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知
            Notification.Builder nb = new Notification.Builder(context,NotificationChannels.LOW)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Simple notification")
                    //设置通知内容
                    .setContentText("Demo for simple notification !")
                    //设置点击通知后自动删除通知
                    .setAutoCancel(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置通知右侧的大图标
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置删除通知时的响应事件
                    .setDeleteIntent(deletePendingIntent);
            //发送通知
            nm.notify(NOTIFICATION_SAMPLE, nb.build());
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setContentTitle("Simple notification")
                    .setContentText("Demo for simple notification !")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置点击通知后自动删除通知
                    .setAutoCancel(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置notification的优先级，优先级越高的，通知排的越靠前，优先级低的，不会在手机最顶部的状态栏显示图标
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置删除通知时的响应事件
                    .setDeleteIntent(deletePendingIntent)
                    //设置通知右侧的大图标
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
                    .setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
            Notification notification = builder.build();
            // 不能被清除
            //notification.flags |= Notification.FLAG_NO_CLEAR;
            nm.notify(NOTIFICATION_SAMPLE, notification);
        }
    }


    public static void sendActionNotification(Context context,NotificationManager nm) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context, NotificationService.class);
        intent.setAction(Constant.ACTION_ACTION);
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);

        //创建删除通知时发送的广播
        Intent deleteIntent = new Intent(context, MainActivity.class);
        PendingIntent deletePendingIntent = PendingIntent.getActivity(context,0,deleteIntent,0);

        //创建点击通知 YES 按钮时发送的广播
        Intent yesIntent = new Intent();
        yesIntent.setAction(Constant.ACTION);
        yesIntent.putExtra(Constant.DATA, "==带Action动作按钮的通知==");
        //FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的 PendingIntent 对象，那么就将先将已有的 PendingIntent 取消，然后重新生成一个 PendingIntent 对象。
        //FLAG_NO_CREATE:如果当前系统中不存在相同的 PendingIntent 对象，系统将不会创建该 PendingIntent 对象而是直接返回 null 。
        //FLAG_ONE_SHOT:该 PendingIntent 只作用一次。
        //FLAG_UPDATE_CURRENT:如果系统中已存在该 PendingIntent 对象，那么系统将保留该 PendingIntent 对象，但是会使用新的 Intent 来更新之前 PendingIntent 中的 Intent 对象数据，例如更新 Intent 中的 Extras 。
        PendingIntent yesPendingIntent = PendingIntent.getBroadcast(context,0,yesIntent,PendingIntent.FLAG_CANCEL_CURRENT);

        //创建点击通知 NO 按钮时发送的广播
        Intent noIntent = new Intent(context,NotificationService.class);
        noIntent.setAction(Constant.ACTION_NO);
        PendingIntent noPendingIntent = PendingIntent.getService(context,0,noIntent,0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知
            Notification.Builder nb = new Notification.Builder(context,NotificationChannels.DEFAULT)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("通知")
                    //设置通知内容
                    .setContentText("带Action按钮通知 !")
                    //设置点击通知后不自动删除通知
                    .setAutoCancel(false)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置删除通知时的响应事件
                    .setDeleteIntent(deletePendingIntent);

            //创建点击通知 YES 按钮时发送的广播
            Notification.Action yesActionBuilder = new Notification.Action.Builder(
                    Icon.createWithResource("", R.drawable.ic_yes),
                    "YES",
                    yesPendingIntent)
                    .build();

            //创建点击通知 NO 按钮时发送的广播
            Notification.Action noActionBuilder = new Notification.Action.Builder(
                    Icon.createWithResource("", R.drawable.ic_no),
                    "NO",
                    noPendingIntent)
                    .build();

            //为通知添加按钮
            nb.setActions(yesActionBuilder, noActionBuilder);
            //发送通知
            nm.notify(NOTIFICATION_ACTION, nb.build());
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setContentTitle("通知")
                    .setContentText("带Action按钮通知 !")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置点击通知后不自动删除通知
                    .setAutoCancel(false)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置notification的优先级，优先级越高的，通知排的越靠前，优先级低的，不会在手机最顶部的状态栏显示图标
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置删除通知时的响应事件
                    .setDeleteIntent(deletePendingIntent)
                    //设置通知右侧的大图标
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
                    .setVisibility(NotificationCompat.VISIBILITY_PRIVATE);

            //创建点击通知 YES 按钮时发送的广播
            NotificationCompat.Action yesAction = new NotificationCompat.Action(R.drawable.ic_yes, "YES", yesPendingIntent);
            //创建点击通知 NO 按钮时发送的广播
            NotificationCompat.Action noAction = new NotificationCompat.Action(R.drawable.ic_no, "NO", noPendingIntent);

            //为通知添加按钮
            builder.addAction(yesAction);
            builder.addAction(noAction);

            //发送通知
            nm.notify(NOTIFICATION_ACTION, builder.build());
        }
    }


    /**
     * 自定义顶部提醒视图通知，部分ROM是不支持
     */
    public static void sendCustomHeadsUpViewNotification(Context context, NotificationManager nm) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context, LaunchActivity.class);
        PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);

        //创建自定义顶部提醒视图
        //接听
        Intent answerIntent = new Intent(context,NotificationService.class);
        answerIntent.setAction(Constant.ACTION_CUSTOM_HEADS_UP_VIEW);
        answerIntent.putExtra(Constant.EXTRA_OPTIONS, Constant.ACTION_ANSWER);
        PendingIntent answerPendingIntent = PendingIntent.getService(context,0,answerIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        //拒接
        Intent rejectIntent = new Intent(context,NotificationService.class);
        rejectIntent.setAction(Constant.ACTION_CUSTOM_HEADS_UP_VIEW);
        rejectIntent.putExtra(Constant.EXTRA_OPTIONS, Constant.ACTION_REJECT);
        PendingIntent rejectPendingIntent = PendingIntent.getService(context,1,rejectIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews headsUpView = new RemoteViews(context.getPackageName(),R.layout.custom_heads_up_layout);
        headsUpView.setOnClickPendingIntent(R.id.iv_answer, answerPendingIntent);
        headsUpView.setOnClickPendingIntent(R.id.iv_reject ,rejectPendingIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知
            Notification.Builder nb = new Notification.Builder(context,NotificationChannels.CRITICAL)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Custom heads up notification")
                    //设置通知内容
                    .setContentText("Demo for custom heads up notification !")
                    //设置点击通知后自动删除通知
                    .setAutoCancel(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置全屏响应事件;
                    .setFullScreenIntent(pi,true)
                    //设置自定义顶部提醒视图
                    .setCustomHeadsUpContentView(headsUpView);
            //发送通知
            nm.notify(NOTIFICATION_CUSTOM_HEADS_UP, nb.build());
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    //设置通知标题
                    .setContentTitle("Custom heads up notification")
                    //设置通知内容
                    .setContentText("Demo for custom heads up notification !")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置点击通知后自动删除通知
                    .setAutoCancel(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置全屏响应事件;
                    .setFullScreenIntent(pi,true)
                    //设置自定义顶部提醒视图
                    .setCustomHeadsUpContentView(headsUpView);
            //发送通知
            nm.notify(NOTIFICATION_CUSTOM_HEADS_UP, builder.build());
        }
    }



    public static void sendProgressViewNotification(Context context, NotificationManager nm, int progress) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context, NotificationService.class);
        intent.setAction(Constant.ACTION_PROGRESS);
        PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);

        Notification.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(context, NotificationChannels.LOW);
        } else {
            builder = new Notification.Builder(context);
        }

        //设置通知左侧的小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置通知标题
        builder.setContentTitle("正在下载...");
        //设置通知内容
        builder.setContentText(String.valueOf(progress) + "%");
        //设置通知不可删除
        builder.setOngoing(true);
        //设置显示通知时间
        builder.setShowWhen(true);
        //设置点击通知时的响应事件
        builder.setContentIntent(pi);
        //设置全屏响应事件，会出现顶部提醒视图
        builder.setFullScreenIntent(pi,true);
        //设置当前进度
        builder.setProgress(100, progress,false);

        //发送通知
        nm.notify(NOTIFICATION_CUSTOM_HEADS_UP, builder.build());
    }



    public static void sendBigPictureStyleNotification(Context context,NotificationManager nm) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context,NotificationService.class);
        intent.setAction(Constant.ACTION_BIG_PICTURE_STYLE);
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);

        //创建大视图样式
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle()
                .setBigContentTitle("Big picture style notification ~ Expand title")
                .setSummaryText("Demo for big picture style notification ! ~ Expand summery")
                .bigPicture(BitmapFactory.decodeResource(context.getResources(),R.drawable.big_style_picture));

        //创建通知
        Notification.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(context, NotificationChannels.DEFAULT);
        } else {
            builder = new Notification.Builder(context);
        }

        //设置通知左侧的小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置通知标题
        builder.setContentTitle("Big picture style notification");
        //设置通知内容
        builder.setContentText("Demo for big picture style notification !");
        //设置点击通知后自动删除通知
        builder.setAutoCancel(true);
        //设置显示通知时间
        builder.setShowWhen(true);
        //设置点击通知时的响应事件
        builder.setContentIntent(pi);
        //设置大视图样式通知
        builder.setStyle(bigPictureStyle);

        //发送通知
        nm.notify(NOTIFICATION_BIG_PICTURE_STYLE, builder.build());
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
    public static void sendRemoteInputNotification(Context context, NotificationManager nm) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context,NotificationService.class);
        intent.setAction(Constant.ACTION_REMOTE_INPUT);
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);

        Intent remoteInputIntent = new Intent(context, NotificationService.class);
        remoteInputIntent.setAction(Constant.ACTION_REPLY);
        PendingIntent replyPendingIntent = PendingIntent.getService(context,2,remoteInputIntent,0);

        //创建通知
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Builder builder = new Notification.Builder(context, NotificationChannels.DEFAULT);
            //设置通知左侧的小图标
            builder.setSmallIcon(R.mipmap.ic_launcher);
            //设置通知标题
            builder.setContentTitle("快速回复通知");
            //设置通知内容
            builder.setContentText("带快速回复功能的通知!");
            //设置点击通知后自动删除通知
            builder.setAutoCancel(true);
            //设置显示通知时间
            builder.setShowWhen(true);
            //设置点击通知时的响应事件
            builder.setContentIntent(pi);

            //创建带输入框的按钮
            RemoteInput remoteInput = new RemoteInput.Builder(Constant.REMOTE_INPUT_RESULT_KEY).setLabel("回复").build();
            Notification.Action replyAction = new Notification.Action.Builder(
                    Icon.createWithResource("", R.drawable.ic_reply),
                    "回复",
                    replyPendingIntent)
                    .addRemoteInput(remoteInput)
                    .build();
            //为通知添加按钮
            builder.setActions(replyAction);
            //发送通知
            nm.notify(NOTIFICATION_REMOTE_INPUT, builder.build());
        } else {
            Notification.Builder builder = new Notification.Builder(context);
            //设置通知左侧的小图标
            builder.setSmallIcon(R.mipmap.ic_launcher);
            //设置通知标题
            builder.setContentTitle("快速回复通知");
            //设置通知内容
            builder.setContentText("带快速回复功能的通知!");
            //设置点击通知后自动删除通知
            builder.setAutoCancel(true);
            //设置显示通知时间
            builder.setShowWhen(true);
            //设置点击通知时的响应事件
            builder.setContentIntent(pi);

            //创建带输入框的按钮
            RemoteInput remoteInput = new RemoteInput.Builder(Constant.REMOTE_INPUT_RESULT_KEY).setLabel("回复").build();
            //创建快捷回复 Action
            Notification.Action replyAction = new Notification.Action.Builder(R.drawable.ic_reply, "回复", pi).addRemoteInput(remoteInput).build();
            //为通知添加按钮
            builder.addAction(replyAction);
            //发送通知
            nm.notify(NOTIFICATION_REMOTE_INPUT, builder.build());
        }
    }


    //多文字效果通知
    public static void sendBigTextStyleNotification(Context context,NotificationManager nm) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context, NotificationService.class);
        intent.setAction(Constant.ACTION_BIG_TEXT_STYLE);
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);

        //创建大文字样式
        Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle()
                .setBigContentTitle("Big text style notification ~ Expand title")
                .setSummaryText("Demo for big text style notification ! ~ Expand summery")
                .bigText("We are the champions   \n" +
                        "We are the champions   \n" +
                        "No time for losers   \n" +
                        "Cause we are the champions of the World");

        //创建通知
        Notification.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(context,NotificationChannels.DEFAULT);
        } else {
            builder = new Notification.Builder(context);
        }

        //设置通知左侧的小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置通知标题
        builder.setContentTitle("Big text style notification");
        //设置通知内容
        builder.setContentText("Demo for big text style notification !");
        //设置点击通知后自动删除通知
        builder.setAutoCancel(true);
        //设置显示通知时间
        builder.setShowWhen(true);
        //设置点击通知时的响应事件
        builder.setContentIntent(pi);
        //设置大文字样式通知
        builder.setStyle(bigTextStyle);
        //发送通知
        nm.notify(NOTIFICATION_BIG_TEXT_STYLE, builder.build());
    }

    //信箱效果通知
    public static void sendInboxStyleNotification(Context context, NotificationManager nm) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context,NotificationService.class);
        intent.setAction(Constant.ACTION_INBOX_STYLE);
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);

        //创建信箱样式
        Notification.InboxStyle inboxStyle = new Notification.InboxStyle()
                .setBigContentTitle("Inbox style notification ~ Expand title")
                .setSummaryText("Demo for inbox style notification ! ~ Expand summery")
                //最多六行
                .addLine("1. I am email content.")
                .addLine("2. I am email content.")
                .addLine("3. I am email content.")
                .addLine("4. I am email content.")
                .addLine("5. I am email content.")
                .addLine("6. I am email content.");
        //创建通知
        Notification.Builder builder = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(context,NotificationChannels.DEFAULT);
        } else {
            builder = new Notification.Builder(context);
        }

        //设置通知左侧的小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置通知标题
        builder.setContentTitle("Inbox style notification");
        //设置通知内容
        builder.setContentText("Demo for inbox style notification !");
        //设置点击通知后自动删除通知
        builder.setAutoCancel(true);
        //设置显示通知时间
        builder.setShowWhen(true);
        //设置点击通知时的响应事件
        builder.setContentIntent(pi);
        //设置信箱样式通知
        builder.setStyle(inboxStyle);
        //发送通知
        nm.notify(NOTIFICATION_INBOX_STYLE, builder.build());
    }

    //媒体效果通知
    public static void sendMediaStyleNotification(Context context, NotificationManager nm, boolean isPlaying) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context,NotificationService.class);
        intent.setAction(Constant.ACTION_MEDIA_STYLE);
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);

        //创建Action按钮
        Intent playOrPauseIntent = new Intent(context,NotificationService.class);
        playOrPauseIntent.setAction(Constant.ACTION_MEDIA_STYLE);
        playOrPauseIntent.putExtra(Constant.EXTRA_OPTIONS, isPlaying ? Constant.MEDIA_STYLE_ACTION_PAUSE : Constant.MEDIA_STYLE_ACTION_PLAY);
        PendingIntent playOrPausePendingIntent = PendingIntent.getService(context,0, playOrPauseIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent nextIntent = new Intent(context,NotificationService.class);
        nextIntent.setAction(Constant.ACTION_MEDIA_STYLE);
        nextIntent.putExtra(Constant.EXTRA_OPTIONS, Constant.MEDIA_STYLE_ACTION_NEXT);
        PendingIntent nextPendingIntent = PendingIntent.getService(context,1, nextIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent deleteIntent = new Intent(context,NotificationService.class);
        deleteIntent.setAction(Constant.ACTION_MEDIA_STYLE);
        deleteIntent.putExtra(Constant.EXTRA_OPTIONS, Constant.MEDIA_STYLE_ACTION_DELETE);
        PendingIntent deletePendingIntent = PendingIntent.getService(context,2, deleteIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.Action playOrPauseAction = new Notification.Action.Builder(
                    Icon.createWithResource(context,isPlaying ? R.drawable.ic_pause : R.drawable.ic_play),
                    isPlaying ? "PAUSE" : "PLAY",
                    playOrPausePendingIntent)
                    .build();

            Notification.Action nextAction = new Notification.Action.Builder(
                    Icon.createWithResource(context,R.drawable.ic_next),
                    "Next",
                    nextPendingIntent)
                    .build();

            Notification.Action deleteAction = new Notification.Action.Builder(
                    Icon.createWithResource(context,R.drawable.ic_delete),
                    "Delete",
                    deletePendingIntent)
                    .build();

            //创建媒体样式，最多三个Action
            Notification.MediaStyle mediaStyle = new Notification.MediaStyle().setShowActionsInCompactView(0,1,2);
            //创建通知
            Notification.Builder nb = new Notification.Builder(context,NotificationChannels.MEDIA)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Media style notification")
                    //设置通知内容
                    .setContentText("Demo for media style notification !")
                    //设置通知不可删除
                    .setOngoing(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置Action按钮
                    .setActions(playOrPauseAction,nextAction,deleteAction)
                    //设置信箱样式通知
                    .setStyle(mediaStyle);

            //发送通知
            nm.notify(NOTIFICATION_MEDIA_STYLE,nb.build());
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            Notification.Action playOrPauseAction = new Notification.Action.Builder(isPlaying ? R.drawable.ic_pause : R.drawable.ic_play,
                    isPlaying ? "PAUSE" : "PLAY",
                    playOrPausePendingIntent)
                    .build();

            Notification.Action nextAction = new Notification.Action.Builder(R.drawable.ic_next,
                    "Next",
                    nextPendingIntent)
                    .build();

            Notification.Action deleteAction = new Notification.Action.Builder(R.drawable.ic_delete,
                    "Delete",
                    deletePendingIntent)
                    .build();

            //创建媒体样式，最多三个Action
            Notification.MediaStyle mediaStyle = new Notification.MediaStyle().setShowActionsInCompactView(0, 1, 2);
            //创建通知
            Notification.Builder nb = new Notification.Builder(context)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Media style notification")
                    //设置通知内容
                    .setContentText("Demo for media style notification !")
                    //设置通知不可删除
                    .setOngoing(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置Action按钮
                    .addAction(playOrPauseAction)
                    .addAction(nextAction)
                    .addAction(deleteAction)
                    //设置信箱样式通知
                    .setStyle(mediaStyle);
            //发送通知
            nm.notify(NOTIFICATION_MEDIA_STYLE,nb.build());
        }
    }

    //信息效果通知
    public static void sendMessagingStyleNotification(Context context, NotificationManager nm) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context,NotificationService.class);
        intent.setAction(Constant.ACTION_MESSAGING_STYLE);
        PendingIntent pi = PendingIntent.getService(context,0,intent,0);
        //创建信息样式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle("peter")
                    .setConversationTitle("Messaging style notification")
                    .addMessage("This is a message for you", System.currentTimeMillis(),"peter");
            //创建通知
            Notification.Builder nb = new Notification.Builder(context,NotificationChannels.DEFAULT)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Messaging style notification")
                    //设置通知内容
                    .setContentText("Demo for messaging style notification !")
                    //设置点击通知后自动删除通知
                    .setAutoCancel(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置信箱样式通知
                    .setStyle(messagingStyle);
            //发送通知
            nm.notify(NOTIFICATION_MESSAGING_STYLE,nb.build());
        } else {
            NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle("peter")
                    .setConversationTitle("Messaging style notification")
                    .addMessage("This is a message for you", System.currentTimeMillis(),"peter");
            //创建通知
            NotificationCompat.Builder nb = new NotificationCompat.Builder(context,NotificationChannels.DEFAULT)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Messaging style notification")
                    //设置通知内容
                    .setContentText("Demo for messaging style notification !")
                    //设置点击通知后自动删除通知
                    .setAutoCancel(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置信箱样式通知
                    .setStyle(messagingStyle);
            //发送通知
            nm.notify(NOTIFICATION_MESSAGING_STYLE,nb.build());
        }
    }


    public static void sendCustomViewNotification(Context context, NotificationManager nm, NotificationContentWrapper content, Boolean isLoved, Boolean isPlaying) {
        //创建点击通知时发送的广播
        Intent intent = new Intent(context, LaunchActivity.class);
        PendingIntent pi = PendingIntent.getActivity(context,0,intent,0);

        //创建各个按钮的点击响应广播
        Intent intentCancel = new Intent(context,NotificationService.class);
        intentCancel.setAction(Constant.ACTION_CUSTOM_VIEW_OPTIONS_CANCEL);
        PendingIntent piCancel = PendingIntent.getService(context,0,intentCancel,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentLove = new Intent(context, NotificationService.class);
        intentLove.setAction(Constant.ACTION_CUSTOM_VIEW_OPTIONS_LOVE);
        PendingIntent piLove = PendingIntent.getService(context,0,intentLove,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentPre = new Intent(context,NotificationService.class);
        intentPre.setAction(Constant.ACTION_CUSTOM_VIEW_OPTIONS_PRE);
        PendingIntent piPre = PendingIntent.getService(context,0,intentPre,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentPlayOrPause = new Intent(context,NotificationService.class);
        intentPlayOrPause.setAction(Constant.ACTION_CUSTOM_VIEW_OPTIONS_PLAY_OR_PAUSE);
        PendingIntent piPlayOrPause = PendingIntent.getService(context,0, intentPlayOrPause,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentNext = new Intent(context,NotificationService.class);
        intentNext.setAction(Constant.ACTION_CUSTOM_VIEW_OPTIONS_NEXT);
        PendingIntent piNext = PendingIntent.getService(context,0,intentNext,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentLyrics = new Intent(context,NotificationService.class);
        intentLyrics.setAction(Constant.ACTION_CUSTOM_VIEW_OPTIONS_LYRICS);
        PendingIntent piLyrics = PendingIntent.getService(context,0,intentLyrics,PendingIntent.FLAG_UPDATE_CURRENT);

        //创建自定义小视图
        RemoteViews customView = new RemoteViews(context.getPackageName(),R.layout.custom_view_layout);
        customView.setImageViewBitmap(R.id.iv_content, content.bitmap);
        customView.setTextViewText(R.id.tv_title, content.title);
        customView.setTextViewText(R.id.tv_summery, content.summery);
        customView.setImageViewBitmap(R.id.iv_play_or_pause,BitmapFactory.decodeResource(context.getResources(),
                isPlaying ? R.drawable.ic_pause : R.drawable.ic_play));
        customView.setOnClickPendingIntent(R.id.iv_play_or_pause,piPlayOrPause);
        customView.setOnClickPendingIntent(R.id.iv_next,piNext);
        customView.setOnClickPendingIntent(R.id.iv_lyrics,piLyrics);
        customView.setOnClickPendingIntent(R.id.iv_cancel,piCancel);

        //创建自定义大视图
        RemoteViews customBigView = new RemoteViews(context.getPackageName(),R.layout.custom_big_view_layout);
        customBigView.setImageViewBitmap(R.id.iv_content_big,content.bitmap);
        customBigView.setTextViewText(R.id.tv_title_big,content.title);
        customBigView.setTextViewText(R.id.tv_summery_big,content.summery);
        customBigView.setImageViewBitmap(R.id.iv_love_big,BitmapFactory.decodeResource(context.getResources(),
                isLoved ? R.drawable.ic_loved : R.drawable.ic_love));
        customBigView.setImageViewBitmap(R.id.iv_play_or_pause_big,BitmapFactory.decodeResource(context.getResources(),
                isPlaying ? R.drawable.ic_pause : R.drawable.ic_play));
        customBigView.setOnClickPendingIntent(R.id.iv_love_big,piLove);
        customBigView.setOnClickPendingIntent(R.id.iv_pre_big,piPre);
        customBigView.setOnClickPendingIntent(R.id.iv_play_or_pause_big,piPlayOrPause);
        customBigView.setOnClickPendingIntent(R.id.iv_next_big,piNext);
        customBigView.setOnClickPendingIntent(R.id.iv_lyrics_big,piLyrics);
        customBigView.setOnClickPendingIntent(R.id.iv_cancel_big,piCancel);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知
            Notification.Builder nb = new Notification.Builder(context,NotificationChannels.MEDIA)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Custom notification")
                    //设置通知内容
                    .setContentText("Demo for custom notification !")
                    //设置通知不可删除
                    .setOngoing(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置自定义小视图
                    .setCustomContentView(customView)
                    //设置自定义大视图
                    .setCustomBigContentView(customBigView);
            //发送通知
            nm.notify(NOTIFICATION_CUSTOM, nb.build());
        } else {
            //创建通知
            NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
                    //设置通知左侧的小图标
                    .setSmallIcon(R.mipmap.ic_launcher)
                    //设置通知标题
                    .setContentTitle("Custom notification")
                    //设置通知内容
                    .setContentText("Demo for custom notification !")
                    //设置通知不可删除
                    .setOngoing(true)
                    //设置显示通知时间
                    .setShowWhen(true)
                    //设置点击通知时的响应事件
                    .setContentIntent(pi)
                    //设置自定义小视图
                    .setCustomContentView(customView)
                    //设置自定义大视图
                    .setCustomBigContentView(customBigView);
            //发送通知
            nm.notify(NOTIFICATION_CUSTOM, nb.build());
        }
    }


    public static class NotificationContentWrapper {
        public Bitmap bitmap;
        public String title;
        public String summery;

        public NotificationContentWrapper(Bitmap bitmap, String title, String summery) {
            this.bitmap = bitmap;
            this.title = title;
            this.summery = summery;
        }
    }
}
